package com.zahb.qadx.ui.view.decoration;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Zdw on 2020/10/10 18:27
 * Item记得添加背景色
 */
public class MDividerItemDecoration extends DividerItemDecoration {

    private boolean mShowLast = true;

    public MDividerItemDecoration(Context context) {
        this(context, VERTICAL);
    }

    public MDividerItemDecoration(Context context, int orientation) {
        super(context, orientation);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getAdapter() == null) return;
        int childCount = parent.getAdapter().getItemCount();
        // 最后一行或最后一列不进行偏移
        if (parent.getChildAdapterPosition(view) == childCount - 1 && !mShowLast) {
            outRect.set(0, 0, 0, 0);
        } else {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }

    public void setShowLast(boolean showLast) {
        mShowLast = showLast;
    }
}
